package com.xyw.code.study202007281.service;

import com.xyw.code.study202007281.entity.Order;
import com.xyw.code.study202007281.handler.OrderHandler;
import com.xyw.code.study202007281.handler.OrderHandlerBO;
import com.xyw.code.study202007281.processor.HandlerContext;
import com.xyw.code.study202007281.processor.HandlerPayMethodContext;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created with IntelliJ IDEA.
 * User: xuyiwei
 * Date: 2020/7/28
 * Time: 下午10:55
 * Email: 1328312923@qq.com
 * Description: No Description
 **/
@Service
public class OrderServiceImpl2 {

   @Autowired
   private HandlerContext context;

   @Autowired
   private HandlerPayMethodContext handlerPayMethodContext;


    public String hander(Order order){
        OrderHandler orderHandler = null;
        try {
            orderHandler = context.getInstance(order.getSource());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return orderHandler.handler(order);
    }

    public String hander2(Order order){
        OrderHandler orderHandler = null;
        try {
            OrderHandlerBO bo = OrderHandlerBO.builder().build();
            BeanUtils.copyProperties(order,bo);
            orderHandler = handlerPayMethodContext.getInstance(bo);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        if(orderHandler!=null){
            return orderHandler.handler(order);
        }
        return null;
    }
}
